package com.danskin.wechat.web;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.model.LoginUser;
import com.danskin.wechat.service.WxFansCardService;
import com.danskin.wechat.util.DateTools;
import com.danskin.wechat.util.ExcelUtil;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Page;
import com.danskin.wechat.vo.WxFansCardDetailVo;
import com.danskin.wechat.vo.WxFansCardVo;

import jxl.write.WritableWorkbook;

@Controller
@RequestMapping("wxOriginCardAnalyze")
public class WxOriginCardAnalyzeController extends BaseController {

	@Autowired
	private WxFansCardService wxFansCardService;

	/**
	 * 跳转到微信原生卡领取统计页面
	 * 
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "jumpAnalyzePage", method = RequestMethod.GET)
	public String jumpAnalyzePage() {
		return "reportStatistics/wx_origin_card_analyze";
	}

	@ResponseBody
	@RequestMapping(value = "selectWxFansCardList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object selectWxFansCardList() {
		List<WxFansCardVo> fansCardList = new ArrayList<WxFansCardVo>();

		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());

			fansCardList = wxFansCardService.selectWxFansCardList(map);
		} catch (Exception e) {
			LogUtil.error("======根据时间查询微信原生卡领取异常", e);
			return new AccessResponse<List<WxFansCardVo>>("-1", "查询异常", null);
		}
		return new AccessResponse<List<WxFansCardVo>>("0", "查询异常", fansCardList);
	}

	@ResponseBody
	@RequestMapping(value = "selectWxFansCardDetailList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object selectWxFansCardDetailList(Page page) {
		List<WxFansCardDetailVo> fansCardDetailList = new ArrayList<WxFansCardDetailVo>();

		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());
			page.setMap(map);

			fansCardDetailList = wxFansCardService.listPageWxFansCardDetailList(page);
		} catch (Exception e) {
			LogUtil.error("======根据时间查询微信原生卡领取详情异常", e);
		}
		return pagelistToJSONForBootStrapTable(fansCardDetailList, page);
	}

	/**
	 * 根据查询条件导出微信原生卡领取excel
	 * 
	 * @param response
	 * @author makunsmac
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "exportExcel", method = RequestMethod.GET)
	public void exportExcel(HttpServletResponse response) {
		OutputStream out = null;

		WritableWorkbook exportExcelWorkBook = null;
		try {
			Map map = getRequestParameter(getRequest());
			LoginUser loginUser = getLoginUser();
			map.put("mpId", loginUser.getMpId());

			LogUtil.info("======根据查询条件导出微信原生卡领取excel参数：" + ToStringBuilder.reflectionToString(map));

			out = response.getOutputStream();

			// 查询符合条件的数据
			List<WxFansCardDetailVo> fansCardDetailList = wxFansCardService.selectWxFansCardDetailList(map);
			// 格式化数据
			for (WxFansCardDetailVo temp : fansCardDetailList) {
				if (temp.getAcquireStatus() == (byte) 1) {
					temp.setAcquireStatusString("已激活");
				} else if (temp.getAcquireStatus() == (byte) 0) {
					temp.setAcquireStatusString("未激活");
				}
			}

			// 封装表头
			LinkedHashMap<String, String> fieldMap = new LinkedHashMap<String, String>();
			fieldMap = packageExcelHead(fieldMap);

			// 执行导出
			exportExcelWorkBook = ExcelUtil.listToExcel(fansCardDetailList, fieldMap, "微信原生卡领取统计", out);

			String fileName = "微信原生卡领取统计-" + new SimpleDateFormat(DateTools.YYYYMMDDHHMMSS).format(new Date()) + ".xls";// 文件名
			response.setContentType("application/x-download");
			response.setCharacterEncoding("utf-8");
			response.setHeader("Content-Disposition",
					"attachment;filename=" + new String(fileName.getBytes("gbk"), "iso8859-1"));
			exportExcelWorkBook.write();
			exportExcelWorkBook.close();
		} catch (Exception e) {
			LogUtil.error("======");
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (Exception ex) {
				LogUtil.error("======导出微信原生卡领取统计流关闭失败", ex);
			}
		}
	}

	/**
	 * 封装表头
	 * 
	 * @param fieldMap
	 * @return
	 * @author makunsmac
	 */
	private LinkedHashMap<String, String> packageExcelHead(LinkedHashMap<String, String> fieldMap) {
		fieldMap.put("nickName", "昵称");
		fieldMap.put("mobile", "手机");
		fieldMap.put("acquireTime", "领取时间");
		fieldMap.put("acquireStatusString", "是否激活");
		
		return fieldMap;
	}

}
